package com.example.qingyiapp.ui.activity;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.qingyiapp.R;
import com.example.qingyiapp.service.presenter.PersenterMannaer;
import com.example.qingyiapp.ui.activity.adapter.Adapter_VP;
import com.example.qingyiapp.ui.activity.fragment.fragment_AddressUI.Fragment_AddressUI;
import com.example.qingyiapp.ui.activity.fragment.fragment_FindUI.Fragment_FindUI;
import com.example.qingyiapp.ui.activity.fragment.fragment_FrientUI.Fragment_FrientUI;
import com.example.qingyiapp.ui.activity.fragment.fragment_MenuUI.Fragment_MenuUI;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {


    @BindView(R.id.tootbar)
    Toolbar tootbar;
    @BindView(R.id.rg)
    RadioGroup rg;
//    @BindView(R.id.drawerLayout)
//    DrawerLayout drawerLayout;
//    @BindView(R.id.toolbar_left)
//    ImageView toolbar_left;
    int drawer;
     private SlidingMenu slidingMenu;

    PersenterMannaer persenterMannaer;
    @BindView(R.id.ra_main)
    RadioButton raMain;
    @BindView(R.id.ra_yizhan)
    RadioButton raYizhan;
    @BindView(R.id.ra_found)
    RadioButton raFound;
    @BindView(R.id.ra_dress)
    RadioButton raDress;
    @BindView(R.id.vp)
    ViewPager vp ;
    @BindView(R.id.slide_title)
    ImageView slide_title;
    @BindView(R.id.slidea_title)
    ImageView slidea_title;

//    @BindView(R.id.weixin)
//    Button weixin;
//
//    @BindView(R.id.weibo)
//    Button weibo;
//
//    @BindView(R.id.qq)
//    Button qq;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        changeImageSize();
        setSupportActionBar(tootbar);

        Window window = this.getWindow();
//设置透明状态栏,这样才能让 ContentView 向上
//        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

//需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//设置状态栏颜色
        window.setStatusBarColor(Color.parseColor("#75A45D"));


        ViewGroup mContentView = (ViewGroup) this.findViewById(Window.ID_ANDROID_CONTENT);
        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
            //注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 使其不为系统 View 预留空间.
            ViewCompat.setFitsSystemWindows(mChildView, false);
        }
        /**
         * Retrofitnetutlis并没有用到Context，但获取实例的时候却要传context，
         * 而且Retrofitnetutlis还是个单例模式，它的生命周期是整个应用的生命周期，
         * 在activity里传个this进去必然导致内存泄露，因为activity一直被持有无法释放。
         *
         * 解决办法就是单例模式里不需要context就不要加，真的需要的话就把构造方法改成
         * mContext ＝ context.getApplicationContext();
         * 使用应用程序的context替代activity的context从而避免内存泄露
         */


        if (raMain.isChecked() == true) {
            raMain.setTextColor(Color.GREEN);
        }

        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new Fragment_MenuUI());
        fragments.add(new Fragment_FrientUI());
        fragments.add(new Fragment_FindUI());
        fragments.add(new Fragment_AddressUI());

        final Adapter_VP adapter_vp = new Adapter_VP(getSupportFragmentManager(), fragments);
        vp.setAdapter(adapter_vp);
        rg.check(R.id.ra_main);


        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                switch (i){
                    case 0:
                        rg.check(R.id.ra_main);
                        break;
                    case 1:

                        rg.check(R.id.ra_yizhan);
                        break;

                    case 2:
                        rg.check(R.id.ra_found);

                        break;
                    case 3:
                        rg.check(R.id.ra_dress);

                        break;
                    default:


                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.ra_main:
                        vp.setCurrentItem(0);
                        break;

                    case R.id.ra_yizhan:
                        vp.setCurrentItem(1);

                        break;

                    case R.id.ra_found:
                        vp.setCurrentItem(2);

                        break;
                    case R.id.ra_dress:
                        vp.setCurrentItem(3);

                        break;
                        default:


                }
            }
        });
//
//        weixin.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(MainActivity.this, "此功能暂未开启", Toast.LENGTH_SHORT).show();
//            }
//        });
//        weibo.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(MainActivity.this, "此功能暂未开启", Toast.LENGTH_SHORT).show();
//            }
//        });
//        qq.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(MainActivity.this, "此功能暂未开启", Toast.LENGTH_SHORT).show();
//            }
//        });
        slidingMenu();





    }

    private void slidingMenu() {
            //创建菜单对象
           slidingMenu=new SlidingMenu(this);
          //设置属性
          //SlidingMenu.RIGHT   SlidingMenu.LEFT
            slidingMenu.setMode(SlidingMenu.LEFT);//设置从哪边出来
//            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);//在屏幕的任何位置左滑都可以显示出来
            slidingMenu.setBehindOffset(425);//单位是像素px 侧滑菜单滑出后，界面剩余的宽度。
           //绑定到指定的Context
            slidingMenu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT);
            //设置布局并且相应布局上面的按钮
        View view = LayoutInflater.from(this).inflate(R.layout.slidemenu, null);
        slide_title .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(drawer==0){
                      slidingMenu.showMenu();
                      drawer=1;
                }else {
                      slidingMenu.showContent();
                      drawer=0;
                }
            }
        });
        slidea_title .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(drawer==0){
                    slidingMenu.showMenu();
                    drawer=1;
                }else {
                    slidingMenu.showContent();
                    drawer=0;
                }
            }
        });
        slidingMenu.setMenu(view);
        slidingMenu.setOnOpenedListener(new SlidingMenu.OnOpenedListener() {
            @Override
            public void onOpened() {
//                Toast.makeText(MainActivity.this, "打开了", Toast.LENGTH_SHORT).show();
            }
        });
        slidingMenu.setOnClosedListener(new SlidingMenu.OnClosedListener() {
            @Override
            public void onClosed() {
//                Toast.makeText(MainActivity.this, "关闭了", Toast.LENGTH_SHORT).show();

            }
        });
    }

    private void changeImageSize() {
        //定义底部标签图片大小
        Drawable drawableFirst = getResources().getDrawable(R.drawable.radiobtn_select);
        drawableFirst.setBounds(0, 0, 69, 69);//第一0是距左右边距离，第二0是距上下边距离，第三69长度,第四宽度
        raMain.setCompoundDrawables(null, drawableFirst, null, null);//只放上面

        Drawable drawableSearch = getResources().getDrawable(R.drawable.radiobtn_selecttwo);
        drawableSearch.setBounds(0, 0, 69, 69);//第一0是距左右边距离，第二0是距上下边距离，第三69长度,第四宽度
        raYizhan.setCompoundDrawables(null, drawableSearch, null, null);//只放上面

        Drawable drawableMe = getResources().getDrawable(R.drawable.radiobtn_selectthree);
        drawableMe.setBounds(0, 0, 69, 69);//第一0是距左右边距离，第二0是距上下边距离，第三69长度,第四宽度
        raFound.setCompoundDrawables(null, drawableMe, null, null);//只放上面

        Drawable drawableDress = getResources().getDrawable(R.drawable.radiobtn_selectfour);
        drawableDress.setBounds(0, 0, 69, 69);//第一0是距左右边距离，第二0是距上下边距离，第三69长度,第四宽度
        raDress.setCompoundDrawables(null, drawableDress, null, null);//只放上面
    }

}
